package overun.selected;

import com.alibaba.excel.util.StringUtils;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import overun.service.ValueSetService;
import overun.utils.SpringUtil;

import javax.annotation.Resource;

@Data
@Slf4j
@Component
public class ExcelSelectedResolve {

	/**
	 * 下拉内容
	 */
	private String[] source;
 
	/**
	 * 设置下拉框的起始行，默认为第二行
	 */
	private int firstRow;
 
	/**
	 * 设置下拉框的结束行，默认为最后一行
	 */
	private int lastRow;
 
	public String[] resolveSelectedSource(ExcelSelected excelSelected) {
		if (excelSelected == null) {
			return null;
		}
 
		// 获取固定下拉框的内容
		String[] source = excelSelected.source();
		if (source.length > 0) {
			return source;
		}
 
		// 获取动态下拉框的内容
		String keyWord = excelSelected.keyWord();
		if (StringUtils.isNotBlank(keyWord)) {
			try {
				// 根据关键字去查询
				ValueSetService valueSetService = SpringUtil.getBean(ValueSetService.class);
				String[] values = valueSetService.getValueByKeyWord(keyWord);

				if (values != null && values.length > 0) {
					return values;
				}
			} catch (Exception e) {
				log.error("解析动态下拉框数据异常", e);
			}
		}
		return null;
	}
 
}